package com.zhouchao.register.strategy;

import com.zhouchao.register.zookeeper.ZookeeperRegisterManager;
import com.zhouchao.rpc.common.Server;

import java.util.List;

/**
 * @Author: zhouchao
 * @Date: 2021/08/15 23:24
 * @Description:
 */
public class MinCastTimeRule implements IRule {
    private static final ZookeeperRegisterManager REGISTER_MANAGER = new ZookeeperRegisterManager();

    @Override
    public Server choose() {
        List<Server> serverList = REGISTER_MANAGER.discover();
        if (serverList.isEmpty()) {
            throw new RuntimeException("没有可用的服务");
        }

        Server chooseServer = null;
        for (Server server : serverList) {
            if (chooseServer == null ||
                    chooseServer.getCastTime() > server.getCastTime()) {
                chooseServer = server;
            }
        }
        return chooseServer;
    }
}
